package com.genius.reservation.supplier.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.genius.reservation.common.ResponseRule;
import com.genius.reservation.driver.service.IDriverInformationService;
import com.genius.reservation.mapper.OrderInformationMapper;
import com.genius.reservation.mapper.SupplierOrderInformationMapper;
import com.genius.reservation.order.entity.OrderInformation;
import com.genius.reservation.reservation.entity.ReservationInformation;
import com.genius.reservation.reservation.service.IReservationInformationService;
import com.genius.reservation.supplier.service.ISupplierService;
import com.genius.reservation.tools.annotation.webLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController()
@RequestMapping("/reservation/supplier")
@Api(tags = "供货商控制器", value = "供货商控制器")
public class SupplierController {
    @Autowired
    IReservationInformationService reservationService;
    @Autowired
    IDriverInformationService driverService;
    @Autowired
    ISupplierService supplierService;

    @Autowired
    @Lazy
    OrderInformationMapper orderMapper;


    @PostMapping("/createReservation")
    @webLog
    @ApiOperation("创建预约返回二维码")
        //ReservationInformation reservation,String[] orderList,String supplierNumber,String supplierName
    ResponseRule createReservation(@RequestBody JSONObject body) {
        String orderNumber = body.getString("orderNumber");
        String supplierNumber = body.getString("supplierNumber");
        String supplierName = body.getString("supplierName");
        return reservationService.createReservation(orderNumber, supplierNumber, supplierName);
    }

    @PostMapping("/getSupplier")
    @webLog
    @ApiOperation("创建预约返回二维码")
        //ReservationInformation reservation,String[] orderList,String supplierNumber,String supplierName
    ResponseRule getSupplier(@RequestBody JSONObject body) {
        String supplierNumber = body.getString("supplierNumber");
        return supplierService.getSupplier(supplierNumber);
    }

    @PostMapping("/submitReservation")
    @webLog
    @ApiOperation("供应商提交订单信息(订单List)")
        //ReservationInformation reservation,String[] orderList,String supplierNumber,String supplierName
    ResponseRule submitReservation(@RequestBody JSONObject body)
        /* formModel: {
            reservationName:'',
            supplierNumber:'',
            supplierName:'',
            itemNum: '',
            number: '',
            month: '',
            time: '',
            cars:{
                carData0:{
                    carCode:'',
                    carType:'',
                    carLength:''
                }
            },
            orderNumbers: []
        }
        */
    {
        String reservationNumber = body.getString("reservationNumber");
        String reservationName = body.getString("reservationName");
        String supplierNumber = body.getString("supplierNumber");
        String supplierName = body.getString("supplierName");
        String itemNum = body.getString("itemNum");
        String number = body.getString("number");
        String month = body.getString("month");
        String time = body.getString("time");
        String cars = body.getString("cars");
        String[] orderNumbers = body.getJSONArray("orderNumbers").toArray(new String[0]);
        return reservationService.submitReservation(reservationNumber, itemNum, number, month, time, cars, orderNumbers,supplierName,supplierNumber,reservationName);
    }

    //小程序二维码暂不能使用
    //原因：不是企业用户
    @PostMapping("/createReservationRetQRcode")
    @webLog
    @ApiOperation("供货商创建预约返回二维码(暂不能使用)")
    ResponseRule createReservationQR(@RequestBody JSONObject body) {
        String reservationNumber = body.getString("reservationNumber");
        String orderNumber = body.getString("orderNumber");
        return reservationService.createReservationRetQRcode(reservationNumber, orderNumber);
    }


    @PostMapping("/getAllReservation")
    @ApiOperation("获取所有预约单")
    ResponseRule getAllReservation(@RequestParam String supplierNumber) {
        return reservationService.findReservationBySupplier(supplierNumber);
    }

    @PostMapping("/getAllReservationFive")
    @ApiOperation("获取五条预约单")
    ResponseRule getAllReservationFive(@RequestParam String supplierNumber) {
        return reservationService.findReservationBySupplierFive(supplierNumber);
    }


    @PostMapping("/getUnReservationOrders")
    @ApiOperation("获取未预约订单")
    ResponseRule getUnReservationOrders(@RequestBody JSONObject body) {
        String supplierNumber = body.getString("supplierNumber");
        return reservationService.getUnReservationOrders(supplierNumber);
    }

    @PostMapping("/getOrderInfo")
    @ApiOperation("获取未预约订单")
    ResponseRule getOrderInfo(@RequestBody JSONObject body) {
        String order = body.getString("orderNumber");
        return ResponseRule.ok(reservationService.getUnReservationOrder(order));
    }


    @PostMapping("/cancelReservation")
    @ApiOperation("供货商取消预约")
    ResponseRule cancelReservation(@RequestBody JSONObject body) {
        String reservationNumber = body.getString("reservationNumber");
        return reservationService.cancelReceiver(reservationNumber);
    }

    @PostMapping("/editReservation")
    @ApiOperation("供货商修改预约")
    @Deprecated
    ResponseRule editReservation(ReservationInformation reservation) {
        return reservationService.editReservation(reservation);
    }

    @PostMapping("/finishReservation")
    @ApiOperation("完成预约单")
    @Deprecated
    ResponseRule finishReservation(@RequestParam String reservationNumber) {
        return reservationService.finishReservationSupplier(reservationNumber);
    }

    @PostMapping("/getReservation")
    @ApiOperation("获取预约单详情")
    ResponseRule getReservation(@RequestBody JSONObject body) {
        ReservationInformation reservationServiceByReservationNumber = reservationService.getByReservationNumber(body.getString("reservationNumber"));
        if (ObjectUtil.isEmpty(reservationServiceByReservationNumber))
            return ResponseRule.no("未找到订单信息");
        return ResponseRule.ok(reservationServiceByReservationNumber);
    }


    @PostMapping("/getAllDriver")
    @ApiOperation("获得所有司机")
    ResponseRule getAllDriver(@RequestParam String supplierNumber) {
        return driverService.getDriverBySupplier(supplierNumber);
    }

    @GetMapping("/getAllMessage/{userNumber}")
    @ApiOperation("获得所有消息")
    ResponseRule getAllMessage(@PathVariable("userNumber") String userNumber) {
        return supplierService.getAllMessage(userNumber);
    }


//    @PostMapping("/getBilOrder")
//    @ApiOperation("获取所有订单但是排除已预约的")
//    ResponseRule getBilOrder(@RequestParam String supplierNumber){
//        return reservationService.getBilOrder(supplierNumber);
//    }


    @PostMapping("/getReservationByOrder")
    ResponseRule getReservationByOrder(@RequestParam String orderNumber) {
        return reservationService.getReservationByOrder(orderNumber);
    }


    @PostMapping("/getBoxamount")
    @webLog
    ResponseRule getBoxamount(@RequestParam String reservationNumber) {
        return reservationService.getBoxamount(reservationNumber);
    }

    @PostMapping("/setOrder")
    @webLog
    ResponseRule setOrder(@RequestBody JSONObject body) {
        return null;
    }

}
